package sv.mecca.modelo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import sv.mecca.form.Clinica2Form;
public class Clinica2Modelo extends Conexion {
	private static final long serialVersionUID = 4747601351291183179L;

	
	public void saveClinica2(Clinica2Form c2) throws SQLException {
		try {
			save("INSERT INTO clinicas(id_tipo_clinica, id_paciente, id_usuario, tratamiento, diagnostico) VALUES("
					+ c2.getId_tipo_clinica()
					+ ", "
					+ c2.getId_paciente()
					+ ", "
					+ c2.getId_usuario()
					+ ", '"
					+ c2.getTratamiento()
					+ "', '" 
					+ c2.getDiagnostico() + "')");
		} catch (Exception e) {
			System.out
					.println("Error al guardar la clinica2 " + e.getMessage());
		}
	}

	/**
	 * Metodo para actualizar la clinica
	 * 
	 * 
	 * @throws SQLException
	 */
	public void updateClinica2(Clinica2Form c2) throws SQLException {
		try {
			update("UPDATE clinicas SET id_tipo_clinica = "
					+ c2.getId_tipo_clinica() + ", id_paciente = "
					+ c2.getId_paciente() + ", id_usuario = "
					+ c2.getId_usuario() + ", tratamiento = '"
					+ c2.getTratamiento() + "', diagnostico = '"
					+ c2.getDiagnostico() + "' WHERE id_clinica = "
					+ c2.getId_clinica());
		} catch (Exception e) {
			System.out.println("Error al actualizar la clinica2 "
					+ e.getMessage());
		}
	}

	/**
	 * Metodo para borrar la clinica
	 * 
	 * 
	 * @throws SQLException
	 */
	public void deleteClinica2(Clinica2Form u) throws SQLException {
		try {
			delete("DELETE FROM clinicas WHERE id_clinica = "
					+ u.getId_clinica());
		} catch (Exception e) {
			System.out.println("Error al eliminar la clinica2 "
					+ e.getMessage());
		}
	}

	/**
	 * Metodo para listar a todas las clinicas
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Clinica2Form> listado() throws SQLException {
		ArrayList<Clinica2Form> c2 = new ArrayList<Clinica2Form>();
		ResultSet rs = executeQueryList("SELECT * FROM clinicas"
				+ " c2 INNER JOIN tipos_clinicas tu ON c2.id_tipo_clinica = tu.id_tipo_clinica"
				+ " inner join pacientes p on c2.id_paciente = p.id_paciente"
				+ " inner join usuarios u on c2.id_usuario = u.id_usuario");
		while (rs.next()) {
			Clinica2Form t = new Clinica2Form(rs.getInt(1), rs.getInt(2),
					rs.getString("tipo_clinica"), rs.getInt(3), rs.getInt(4),
					rs.getString("id_usuario"), rs.getString(5),
					rs.getString(6));
			c2.add(t);
		}
		return c2;
	}
}
